package com.atguigu.guli.service.trade.service.impl;

import com.atguigu.guli.common.base.result.ResultCodeEnum;
import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.feign.EduCourseService;
import com.atguigu.guli.service.trade.feign.UcenterService;
import com.atguigu.guli.service.trade.mapper.OrderMapper;
import com.atguigu.guli.service.trade.service.OrderService;
import com.atguigu.guli.service.trade.util.OrderNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-10-08
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private UcenterService ucenterService;

    @Override
    public String saveOrder(String courseId, String userId) {

        // 查询当前用户是否已有当前课程的订单
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("course_id",courseId).eq("member_id",userId);
        List<Order> orders = baseMapper.selectList(orderQueryWrapper);
        if(!ObjectUtils.isEmpty(orders)){
            return orders.get(0).getOrderNo();
        }

        // 查询课程信息
        CourseDto courseDto = eduCourseService.getCourseDtoById(courseId);
        if(courseDto == null){
            throw new GuliException(ResultCodeEnum.PAY_UNIFIEDORDER_ERROR);
        }

        // 查询用户信息
        MemberDto memberDto = ucenterService.getMemberDtoByMemberId(userId);
        if(memberDto == null){
            throw new GuliException(ResultCodeEnum.PAY_UNIFIEDORDER_ERROR);
        }

        // 创建订单
        Order order = new Order();
        // 生成订单号
        String orderNo = OrderNoUtils.getOrderNo();
        // 课程信息
        order.setOrderNo(orderNo);
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        order.setTotalFee(courseDto.getPrice().multiply(new BigDecimal(100)));// 单位： 分

        // 会员信息
        order.setMemberId(userId);
        order.setMobile(memberDto.getMobile());
        order.setNickname(memberDto.getNickname());

        // 未支付
        order.setStatus(0);
        order.setPayType(1);// 微信支付
        // 存入数据库
        baseMapper.insert(order);

        return orderNo;

    }

    @Override
    public Order getByOrderId(String orderId, String member) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_no",orderId).eq("member_id",member);
        Order order = baseMapper.selectOne(orderQueryWrapper);

        if(order == null){
            throw new GuliException(ResultCodeEnum.PAY_ORDERQUERY_ERROR);
        }
        return order;
    }

    @Override
    public Boolean isBuyCouseId(String courseId, String memberId) {

        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper
                .eq("member_id",memberId)
                .eq("course_id",courseId)
                .eq("status",1); // 1:为已购买
        Integer integer = baseMapper.selectCount(orderQueryWrapper);

        return integer.intValue() > 0;
    }

    @Override
    public List<Order> selectByMemberId(String memberId) {

        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper
                .orderByDesc("gmt_create")
                .eq("member_id",memberId);

        List<Order> orderList = baseMapper.selectList(orderQueryWrapper);

        return orderList;

    }

    @Override
    public boolean removeByOrderId(String orderId, String memberId) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper
                .eq("member_id",memberId)
                .eq("order_no",orderId);

        return remove(orderQueryWrapper);
    }
}
